package com.ts.api.module.match.plan.internal.distribute.strategy;

import com.gitee.apanlh.exp.ParamParserException;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.common.constant.productflow.ProductApplyFlowStatusEnum;
import com.ts.api.module.api.convert.core.http.client.ApiHttpMessage;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;
import com.ts.api.module.api.service.ApiBizRequiredService;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistribute;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeField;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributePhoneMd5Field;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributePost;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeResult;
import com.ts.api.module.match.plan.internal.distribute.model.PlanDistributeUserRequest;
import com.ts.api.module.product.entity.po.ProductApplyFlowPO;
import com.ts.api.module.product.entity.po.ProductPO;
import com.ts.api.module.product.entity.po.ProductPlanPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *  自有渠道-机构计划分发
 *
 *  @author Pan
 */
@Service
public class ChannelOrgPhoneMd5Distribute implements PlanDistributeStrategy<ApiOrgPhoneMd5ConvertHandler> {

    @Autowired
    private BaseDistribute baseDistribute;
    @Autowired
    private ApiBizRequiredService apiBizRequiredService;

    @Override
    public PlanDistributeResult preValidate(PlanDistribute planDistribute) {
        //  基础校验
        PlanDistributeResult planDistributeResult = baseDistribute.preValidate(planDistribute);
        if (!planDistributeResult.getDistributeStatus()) {
            return planDistributeResult;
        }

        //  md5模式验证是否存在排重库
        if (!baseDistribute.validExistMd5Filter(planDistribute)) {
            return PlanDistributeResult.ofFail(planDistribute, "排重包中已发现该用户");
        }
        return planDistributeResult;
    }

    @Override
    public PlanDistributeResult distribute(PlanDistribute planDistribute) {
        return baseDistribute.distribute(planDistribute);
    }

    @Override
    public PlanDistributeField parseResponse(PlanDistribute planDistribute, ApiHttpMessage apiHttpMessage, ApiOrgPhoneMd5ConvertHandler handler) throws Exception {
        PlanDistributeUserRequest planDistributeUserRequest = planDistribute.getPlanDistributeUserRequest();
        String phoneMd5 = planDistributeUserRequest.getPhoneMd5();
        String reqId = planDistribute.getReqId();
        ProductPO productPO = planDistribute.getProductPO();
        ProductPlanPO productPlanPO = planDistribute.getProductPlanPO();
        ProductApplyFlowPO currentDistributeFlow = planDistribute.getCurrentDistributeFlow();
        String responseBody = apiHttpMessage.getResponseBody();

        //  验证非空情况
        if (ValidParam.isEmpty(responseBody)) {
            throw new ParamParserException("解析失败-响应体返回空");
        }

        //  解析响应体
        RespOrgPhoneMd5DataExchange resp = handler.respDataExchange(responseBody);
        if (ValidParam.isNull(resp)) {
            throw new ParamParserException("撞库参数异常:API机构-md5模式未返回[RespOrgPhoneMd5DataExchange]请检查");
        }

        //  验证返回业务消息是否成功
        if (!resp.getHasBizSuc()) {
            apiBizRequiredService.hasHttpRespBizSuc(currentDistributeFlow, apiHttpMessage, true, ProductApplyFlowStatusEnum.DATA_EXCHANGE_FAIL);
            return PlanDistributeField.ofFail("API机构-md5模式-处理响应结果-对方返回结果[失败]");
        }

        //  撞库流程http响应保存->序列化解析对象
        currentDistributeFlow.setRespData(JsonUtils.toJson(resp));

        //  构建解析后实体
        PlanDistributeField planDistributeField = PlanDistributeField.ofOrgPhoneMd5(productPO, productPlanPO, reqId);

        //  设置phoneMd5
        PlanDistributePhoneMd5Field phoneMd5Field = planDistributeField.getPhoneMd5Field();
        phoneMd5Field.setPhoneMd5(phoneMd5);
        return planDistributeField;
    }

    @Override
    public PlanDistributeResult post(PlanDistributePost planDistributePost) {
        return PlanDistributeResult.ofSuc(planDistributePost);
    }
}
